package cn.edu.zafu.bigdata.Yum;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            List<String> tables = DatabaseManager.getTables();
            System.out.println("数据库中的表有：");
            for (int i = 0; i < tables.size(); i++) {
                System.out.println((i + 1) + ". " + tables.get(i));
            }
            System.out.print("请选择一个表 (输入编号): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            String selectedTable = tables.get(choice - 1);
            System.out.println("您选择的表是：" + selectedTable);

            while (true) {
                System.out.println("======= 学生信息管理系统 =======");
                System.out.println("1. 添加学生信息");
                System.out.println("2. 检索学生信息");
                System.out.println("3. 更新学生信息");
                System.out.println("4. 删除学生信息");
                System.out.println("5. 退出程序");
                System.out.print("请输入操作编号 (1-5): ");
                int option = scanner.nextInt();
                scanner.nextLine(); // Consume newline

                switch (option) {
                    case 1:
                        addStudent(scanner);
                        break;
                    case 2:
                        searchStudent(scanner);
                        break;
                    case 3:
                        updateStudent(scanner);
                        break;
                    case 4:
                        deleteStudent(scanner);
                        break;
                    case 5:
                        System.out.println("退出程序。");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效的选项，请重新输入。");
                }
            }
        } catch (SQLException e) {
            System.out.println("数据库操作失败。");
            e.printStackTrace();
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try {
            if (DatabaseManager.getStudent(id) != null) {
                System.out.println("学号重复，请重新输入。");
                return;
            }
            DatabaseManager.addStudent(new Student(id, name, phone));
            System.out.println("学生信息添加成功。");
        } catch (SQLException e) {
            System.out.println("数据库操作失败。");
            e.printStackTrace();
        }
    }

    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行检索: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.println(student);
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败。");
                e.printStackTrace();
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行更新: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    System.out.print("请输入新的姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入新的电话: ");
                    String phone = scanner.nextLine();
                    student.setName(name);
                    student.setPhone(phone);
                    DatabaseManager.updateStudent(student);
                    System.out.println("学生信息更新成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败。");
                e.printStackTrace();
            }
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号进行删除: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try {
                Student student = DatabaseManager.getStudent(id);
                if (student != null) {
                    DatabaseManager.deleteStudent(id);
                    System.out.println("学生信息删除成功。");
                } else {
                    System.out.println("未找到该学生。");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作失败。");
                e.printStackTrace();
            }
        }
    }
}
